package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.constant.NoticeDefaultStatusConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.NoticeAlreadyExistsException;
import com.mfl.mapper.NoticeMapper;
import com.mfl.pojo.dto.NoticeDTO;
import com.mfl.pojo.dto.NoticePageQueryDTO;
import com.mfl.pojo.entity.Notice;
import com.mfl.pojo.vo.NoticePageQueryVO;
import com.mfl.pojo.vo.NoticeVO;
import com.mfl.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 分页查询公告栏信息
     * @param noticePageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(NoticePageQueryDTO noticePageQueryDTO) {
        PageHelper.startPage(noticePageQueryDTO.getPage(), noticePageQueryDTO.getPageSize());
        Page<NoticePageQueryVO> page = noticeMapper.pageQuery(noticePageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 添加公告栏信息
     * @param noticeDTO
     */
    @Override
    public void insert(NoticeDTO noticeDTO) {
        // 判断标题是否已存在
        Notice notice = noticeMapper.selectByTitle(noticeDTO.getTitle());
        if(notice != null){
            throw new NoticeAlreadyExistsException(MessageConstant.NOTICE_ALREADY_EXISTS);
        }
        Notice build = Notice.builder()
                .title(noticeDTO.getTitle())
                .content(noticeDTO.getContent())
                .status(NoticeDefaultStatusConstant.NOTICE_DEFAULT_STATUS)
                .createTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .build();
        noticeMapper.insert(build);
    }

    /**
     * 批量删除公告栏信息
     * @param ids
     */
    @Override
    public void batchDelete(List<Integer> ids) {
        noticeMapper.deleteByIds(ids);
    }

    /**
     * 修改公告栏信息
     * @param id
     */
    @Override
    public void updateStatus(int id) {
        noticeMapper.updateStatusById(id);
    }

    /**
     * 根据id查询公告栏信息
     * @param id
     * @return
     */
    @Override
    public NoticeVO getNoticeById(int id) {
        Notice notice = noticeMapper.selectById(id);
        return NoticeVO.builder()
                .id(notice.getId())
                .title(notice.getTitle())
                .content(notice.getContent())
                .build();
    }

    /**
     * 修改公告栏信息
     * @param noticeDTO
     */
    @Override
    public void update(NoticeDTO noticeDTO) {
        Notice notice = Notice.builder()
                .id(noticeDTO.getId())
                .title(noticeDTO.getTitle())
                .content(noticeDTO.getContent())
                .updateTime(LocalDateTime.now())
                .updateUser(BaseContext.getCurrentId())
                .build();
        noticeMapper.update(notice);
    }

    /**
     * 获取启用状态的公告栏信息
     * @return
     */
    @Override
    public NoticeVO getNoticeEnable() {
        Notice notice = noticeMapper.getNoticeEnable();
        return NoticeVO.builder()
                .id(notice.getId())
                .title(notice.getTitle())
                .content(notice.getContent())
                .createTime(notice.getCreateTime())
                .build();
    }
}
